﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Pong_Project.Game_objects
{
    class Ball : GameObjectMovable
    {
        private Bat batLastHit;
        private float maxVelocityX, maxVelocityY;


        public Ball(GameObjectIdentity identity, Vector2 position, Vector2 velocity, float speed)
            : base(identity, position, velocity, speed)
        {
            batLastHit = null;
            maxVelocityX = 2.5f;
            maxVelocityY = 2.0f;
            LayerDepth = 0.99f;
        }

        public Bat BatLastHit
        {
            get { return batLastHit; }
            set { batLastHit = value; }
        }

        public float getMaxVelocityX()
        {
            return maxVelocityX;
        }

        public float getMaxVelocityY()
        {
            return maxVelocityY;
        }

        public void BounceOffBat(Bat bat)
        {
            VelocityX *= -1.05f;
            if (Math.Abs(VelocityX) > maxVelocityX)
            {
                if (bat.Identity == GameObjectIdentity.BatRight)
                    VelocityX = -maxVelocityX;
                else
                    VelocityX = maxVelocityX;
            }
            VelocityY = (this.MiddleY - bat.MiddleY) / (bat.Height / 2) * maxVelocityY;
        }


        public void Reset(float velocityX)
        {
            PositionX = Game.WindowWidth / 2 - Width / 2;
            PositionY = Game.WindowHeight / 2 - Height / 2;
            VelocityX = velocityX;
            VelocityY = 0;
        }

        public void Move()
        {
            PositionX += Speed * VelocityX;
            PositionY += Speed * VelocityY;
        }

        public override void Update(GameTime gameTime)
        {
            // Update the balls position. This must be done as the last thing before base.Update() is called.
            Move();

            base.Update(gameTime);
        }

        public override void Reset()
        {
            PositionX = Game.WindowWidth / 2 - Width / 2;
            PositionY = Game.WindowHeight / 2 - Height / 2;
            VelocityX = 1.0f;
            VelocityY = 0.0f;
            ScaleX = 1.0f;
            ScaleY = 1.0f;
            Speed = 5.0f;
            Rotation = 0.0f;
            LayerDepth = 0.99f;
        }
    }
}
